package main

import (
	"go-scripts/models"
	"gopkg.in/mgo.v2/bson"
	"time"
	"fmt"
	"go-scripts/services"
	"log"
	"runtime/debug"
	"sync"
	"runtime"
	"strconv"
)

//获取所有题材
func getEffectTopics(effectTopics *[]*models.EffectTopic) error {
	model, err := models.GetModel()
	if err != nil {
		return err
	}
	effectTopicDao, err := model.GetModelCollection("EffectTopic")
	if err != nil {
		return err
	}
	return effectTopicDao.Find(bson.M{
		"rtype": "theme",
		"disabled": bson.M{
			"$ne": true,
		},
	}).Select(bson.M{
		"hylandaId": 1,
		"rid": 1,
	}).All(effectTopics)
}
//根据hylandaId获取题材
func getThemeByHylandaIds(hylandaId string, theme *models.Theme) error {
	model, err := models.GetModel()
	if err != nil {
		return err
	}
	effectTopicDao, err := model.GetModelCollection("EffectTopic")
	themeDao, err := model.GetModelCollection("Theme")
	effectTopic := new(models.EffectTopic)
	if err != nil {
		return err
	}
	effectTopicDao.Find(bson.M{
		"hylandaId": hylandaId,
	}).Select(bson.M{
		"hylandaId": 1,
		"rid": 1,
	}).One(effectTopic)
	iCode := effectTopic.Rid
	return themeDao.Find(bson.M{
		"iCode": iCode,
	}).One(theme)
}

//获取1-6月的所有日期
func getAllDates() []string {
	unix := time.Date(2017, 1, 1, 0,0, 0, 0, time.Local).Unix()
	dates := []string{}
	for {
		date := time.Unix(unix, 0).Format("20060102")
		dates = append(dates, date)
		if date == "20170630" {
			break
		}
		unix += int64(24 * 3600 * time.Second.Seconds())
	}
	return dates
}
//查询一个题材某个月最高的影响力
func getOneThemeMastEffect(month int, effectStrs []interface{}) float64 {
	var max float64 = 0
	begin, end := 0 ,30
	switch month {
	case 1:
		begin = 0
		end = 30
		break
	case 2:
		begin = 31
		end = 58
		break
	case 3:
		begin = 59
		end = 89
		break
	case 4:
		begin = 90
		end = 119
		break
	case 5:
		begin = 120
		end = 150
		break
	case 6:
		begin = 151
		end = 180
		break
	default:
		begin = 0
		end = 30
	}
	for i := begin; i <= end; i++ {
		var effect float64 = 0
		if effectStrs[i] == nil {
			effect = 0
		} else {
			effect, _ = strconv.ParseFloat(effectStrs[i].(string), 64)
		}
		if effect > max {
			max = effect
		}
	}
	return max
}
//查询一个月影响力最高的题材
func getOneMonthMastEffectTheme(month int, hylandaEffects map[string][]float64) (string, float64) {
	var max float64 = 0
	var maxHylandaId string = ""
	for hylandaId, effects := range hylandaEffects {
		effect := effects[month - 1]
		if effect > max {
			max = effect
			maxHylandaId = hylandaId
		}
	}
	return maxHylandaId, max
}
//获取一个月的涨幅
func getZhangFuByECode(eCode string, month int) (float64, error) {
	model, err := models.GetModel()
	if err != nil {
		return 0, err
	}
	themePointDao, err := model.GetModelCollection("ThemePoint")
	begin := time.Date(2017, time.Month(month), 1, 0, 0, 0, 0, time.Local).Unix()
	end := time.Date(2017, time.Month(month + 1), 1, 0, 0, 0, 0, time.Local).Unix()
	themePoints := []*models.ThemePoint{}
	themePointDao.Find(bson.M{
		"eCode": eCode,
		"time": bson.M{
			"$gte": begin,
			"$lt": end,
		},
	}).All(&themePoints)
	var max float64 = 0
	for _, v := range themePoints {
		if v.ZhangFu > max {
			max = v.ZhangFu
		}
	}
	return max, nil
}

func main()  {
	defer func() {
		if err := recover(); err != nil {
			log.Fatal(err)
			debug.PrintStack()
		}
	}()
	runtime.GOMAXPROCS(runtime.NumCPU())
	dates := getAllDates()
	redisService := services.GetRedisService()
	var err error
	//获取所有题材的hylandaId
	effectTopics := []*models.EffectTopic{}
	getEffectTopics(&effectTopics)
	//遍历每一个hylandaId，获取历史影响力
	effects := make(map[string][]interface{})
	var wg sync.WaitGroup
	var mutex sync.Mutex
	for _, effectTopic := range effectTopics {
		if effectTopic.HylandaId != "" {
			wg.Add(1)
			go func(hylandaId string) {
				defer wg.Done()
				results, _ := redisService.GetEffectByHylandaIdAndDates(hylandaId, dates)
				mutex.Lock()
				{
					effects[hylandaId] = results
				}
				mutex.Unlock()
			}(effectTopic.HylandaId)
		}
	}
	wg.Wait()
	//查询每个题材一到6月影响力的最高值
	effectEachMonthEffectsMap := make(map[string][]float64)
	for key, res := range effects {
		oneHylandaMastEffects := make([]float64, 6)
		oneHylandaMastEffects[0] = getOneThemeMastEffect(1, res)
		oneHylandaMastEffects[1] = getOneThemeMastEffect(2, res)
		oneHylandaMastEffects[2] = getOneThemeMastEffect(3, res)
		oneHylandaMastEffects[3] = getOneThemeMastEffect(4, res)
		oneHylandaMastEffects[4] = getOneThemeMastEffect(5, res)
		oneHylandaMastEffects[5] = getOneThemeMastEffect(6, res)
		effectEachMonthEffectsMap[key] = oneHylandaMastEffects
	}
	hylanda2EffectMap := make(map[string]float64)
	ids := []string{}
	for i := 1; i <= 6; i ++ {
		id := ""
		var effect float64 = 0
		id, effect = getOneMonthMastEffectTheme(i, effectEachMonthEffectsMap)
		hylanda2EffectMap[id] = effect
		ids = append(ids, id)
	}
	fmt.Println(hylanda2EffectMap)
	ids = []string{"10731067950110", "10731067950388", "10731099830311", "10731102943659", "10731067950235", "10731067950151"}
	for i, id := range ids {
		theme := new(models.Theme)
		getThemeByHylandaIds(id, theme)
		zhangFu, _ := getZhangFuByECode(theme.ECode, i + 1)
		fmt.Println(theme.Name, theme.ECode, id, hylanda2EffectMap[id], zhangFu)
	}
	fmt.Println("结束")
	//results, err :=redisService.GetEffectByHylandaIdAndDates("10731070656601", dates)
	if err != nil {
		panic(err.Error())
	}
}